package com.anjubao.parking.municipal.user.controller;


import com.anjubao.parking.municipal.common.dto.ApiResult;
import com.anjubao.parking.municipal.common.exception.ServiceException;
import com.anjubao.parking.municipal.user.dto.DateCustomerDto;
import com.anjubao.parking.municipal.user.service.ParkingCustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.List;

/**
 * 会员controller
 *
 * @author zhangyunbo
 */
@RestController
@RequestMapping("/parking-stat-customers")
@Slf4j
@Api(value="Parkinglot-会员模块",tags = {"运行数据-会员统计"})
public class ParkingStatCustomerController {
    @Autowired
    private ParkingCustomerService parkingCustomerService;

    /**
     【运营数据】-2 会员总数  参数【开始日期、结束日期】
     parking_customer 按天统计，where日期范围统计 必须连续，无数据为0
     /gateway/parking-order-api/parking-stat-customers/day
     */
    @ApiOperation("会员总数 按天统计")
    @GetMapping("/day")
    public ApiResult<List<DateCustomerDto>> day(@RequestParam(name = "startTime", required = false) LocalDate startTime,
                                                @RequestParam(name = "endTime", required = false) LocalDate endTime)
    {
        if(startTime.isAfter(endTime)){
            throw new ServiceException("开始时间不能大于结束时间");
        }
        if (null == startTime) {
            startTime = LocalDate.now().minusDays(100L);
        }
        if (null == endTime){
            endTime = LocalDate.now();
        }
        return ApiResult.success(parkingCustomerService.day(startTime,endTime));
    }

    @ApiOperation("会员总数 按月统计")
    @GetMapping("/month")
    public ApiResult<List<DateCustomerDto>> month(@RequestParam(name = "startTime", required = false) LocalDate startTime,
                                                @RequestParam(name = "endTime", required = false) LocalDate endTime)
    {
        if(startTime.isAfter(endTime)){
            throw new ServiceException("开始时间不能大于结束时间");
        }
        if (null == startTime) {
            startTime = LocalDate.now().minusDays(100L);
        }
        if (null == endTime){
            endTime = LocalDate.now();
        }
        return ApiResult.success(parkingCustomerService.month(startTime,endTime));
    }

    @ApiOperation("会员总数 按年统计")
    @GetMapping("/year")
    public ApiResult<List<DateCustomerDto>> year(@RequestParam(name = "startTime", required = false) LocalDate startTime,
                                                @RequestParam(name = "endTime", required = false) LocalDate endTime)
    {
        if(startTime.isAfter(endTime)){
            throw new ServiceException("开始时间不能大于结束时间");
        }
        if (null == startTime) {
            startTime = LocalDate.now().minusDays(100L);
        }
        if (null == endTime){
            endTime = LocalDate.now();
        }
        return ApiResult.success(parkingCustomerService.year(startTime,endTime));
    }

    @ApiOperation("会员总数 ")
    @GetMapping("/total")
    public ApiResult<String> total()
    {
        return ApiResult.success(parkingCustomerService.total());
    }


}
